home *** CD-ROM | disk | FTP | other *** search
- /*
- * List abstract class - alternate kind using Collect as SuperClass
- *
- * Andrew Nicholson - Not-So-Soft, Oz.
- *
- * Superclasses:
- *
- * Collect
- *
- * methods:
- *
- * head l - returns first item in list l
- * tail l - returns list l minus 1st item
- * push l item - pushes item on front of l
- * isEmpty l - returns non-zero if list is empty, ,zero if not
- * eq l1 l2 - test isomorphic equality between l1 & l2
- *
- */
-
- #include "oic.h"
- #include "generics.h"
-
- class List2;
-
- typedef struct /* list instance structure */
- {
- object l_next;
- object l_item;
- } list_i;
-
- /* -------------------- List Instance methods ---------------------------- */
-
- static object
- _head(self, list) /* return first item in the list */
- object self;
- register list_i *list;
- {
- return list->l_item;
- }
-
- static object
- _tail(self, list) /* return list following the head */
- object self;
- register list_i *list;
- {
- return (list->l_item == END) ? self : list->l_next;
- }
-
- static object
- _push(self, list, item) /* put the item at the start of list */
- object self;
- register list_i *list;
- object *item;
- {
- register object newl;
- register list_i *newinst;
-
- newl = New(List2);
- newinst = localIVs(newl, list_i);
- newinst->l_next = list->l_next;
- newinst->l_item = list->l_item;
- list->l_next = newl;
- list->l_item = *item;
-
- return self;
- }
-
- static int
- _isEmpty(self, list) /* does list have no items in it */
- object self;
- register list_i *list;
- {
- return (list->l_item == END);
- }
-
- static int
- _eq(self, list, otherlist)
- object self;
- list_i *list;
- object *otherlist;
- {
- register object head1, head2;
-
- head1 = head(self);
- head2 = head(*otherlist);
-
- if (head1 == END && head2 == END) /* This should use "isEmpty" */
- return 1;
- if (head1 == END || head2 == END)
- return 0;
- if ((int)eq(head1, head2))
- return ((int)eq(tail(self), tail(*otherlist)));
- else
- return 0;
- }
-
- /* ------------------- Init the List class ------------------------------- */
-
- _InitList2()
- {
- List2 = NewClass(sizeof(list_i), 0, "List2", Collect, END);
- AddMethods(List2,
- headGeneric, _head,
- tailGeneric, _tail,
- pushGeneric, _push,
- isEmptyGeneric, _isEmpty,
- eqGeneric, _eq,
- END
- );
- }
-
-